# TCP并发服务端
import socketserver


class MyRequestHandle(socketserver.BaseRequestHandler):
    def handle(self):
        # tcp协议 self.request=>conn
        # self.client_address =》 address
        while True:
            try:
                data = self.request.recv(1024)
                if len(data) == 0:
                    break

                print("客户端发来的消息:", data.decode('utf-8'))
                self.request.send(data.upper())
            except Exception:
                break
        self.request.close()


s = socketserver.ThreadingTCPServer(('127.0.0.1', 8080), MyRequestHandle)
s.serve_forever()
# 创建链接循环，
# 每次把新的连接封装成为MyRequestHandle对象传入线程
# 执行对象的handle方法


# UDP并发
# class MyRequestHandle(socketserver.BaseRequestHandler):
#     def handle(self):
#         pass
# s = socketserver.ThreadingUDPServer(('127.0.0.1', 8080), MyRequestHandle)
# s.serve_forever()
